Partial Class pages_EmailTemplates_EmailTemplate : Inherits CPage

#Region "Querystring"
    Public ReadOnly Property TemplateId() As Integer
        Get
            Return CWeb.RequestInt("templateId")
        End Get
    End Property
    Public ReadOnly Property IsEdit() As Boolean
        Get
            Return TemplateId <> Integer.MinValue
        End Get
    End Property
#End Region

#Region "Members"
    Private m_emailTemplate As CEmailTemplate
#End Region

#Region "Data"
    Public ReadOnly Property [EmailTemplate]() As CEmailTemplate
        Get
            If IsNothing(m_emailTemplate) Then
                If IsEdit Then
                    m_emailTemplate = CEmailTemplate.Cache.GetById(TemplateId)
                    If IsNothing(m_emailTemplate) Then CSitemap.RecordNotFound("EmailTemplate", TemplateId)
                Else
                    m_emailTemplate = New CEmailTemplate
                    'Inserts: set parentId here (if applicable)
                End If
            End If
            Return m_emailTemplate
        End Get
    End Property
#End Region

#Region "Navigation"
    Private Sub Refresh()
        Response.Redirect(CSitemap.EmailTemplateEdit(Me.EmailTemplate.TemplateId))
    End Sub
    Private Sub ReturnToList()
        Response.Redirect(CSitemap.EmailTemplates)
    End Sub
#End Region

#Region "Event Handlers - Page"
    Protected Overrides Sub PageInit()
        'Dropdonws
        ddTemplate.DataSource = CEmailTemplate.Cache
        ddTemplate.DataBind()
        CDropdown.SetValue(ddTemplate, TemplateId)

        'Page Title
        If IsEdit Then
            Me.Title = "Template Details"
            AddMenuSide("Preview", CSitemap.EmailTemplatePreview(TemplateId))
        Else
            Me.Title = "Create New Template"
        End If

        'Textbox logic
        'Me.Form.DefaultFocus = txtEmailTemplateName.Textbox.ClientID;
        Me.Form.DefaultButton = btnSave.UniqueID   'txtEmailTemplateName.OnReturnPress(btnSave)


        'Button Text
        btnDelete.Visible = IsEdit AndAlso CSession.IsDeveloper
        If IsEdit Then btnCancel.Text = "Back" Else btnSave.Text = "Create EmailTemplate"
        If IsEdit AndAlso CSession.IsDeveloper Then AddButton(CSitemap.EmailTemplateAdd(), "Create a new EmailTemplate")
        If IsEdit Then txtTemplateSubject.Visible = EmailTemplate.TemplateHasSubject
        If Not CSession.IsDeveloper Then
            txtTemplateName.Mode = EControlMode.Locked
            chkTemplateHasSubject.Visible = False
            chkTemplateIsHtml.Visible = False
            txtTemplateTags.Mode = EControlMode.Locked
        End If

    End Sub
    Protected Overrides Sub PageLoad()
        If Page.IsPostback Then Exit Sub

        LoadEmailTemplate()
    End Sub
#End Region

#Region "Event Handlers - Form"
    Private Sub btnSave_Click(ByVal sender As Object, ByVal e As System.EventArgs) Handles btnSave.Click
        If Not Me.IsValid() Then Exit Sub
        SaveEmailTemplate()
        Refresh()
    End Sub
    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        ReturnToList()
    End Sub
    Private Sub btnDelete_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnDelete.Click
        Me.EmailTemplate.Delete()
            'CCache.ClearCache()
        ReturnToList()        
    End Sub
    Protected Sub ddTemplate_SelectedIndexChanged(sender As Object, e As System.EventArgs) Handles ddTemplate.SelectedIndexChanged
        Response.Redirect(CSitemap.EmailTemplateEdit(CDropdown.GetInt(ddTemplate)))
    End Sub
#End Region

#Region "Private - Load/Save"
    Protected Sub LoadEmailTemplate()
        With Me.EmailTemplate
            txtTemplateName.Text = .TemplateName
            chkTemplateHasSubject.Checked = .TemplateHasSubject
            txtTemplateSubject.Text = .TemplateSubject
            chkTemplateIsHtml.Checked = .TemplateIsHtml
            txtTemplateBody.Text = .TemplateBody
            txtTemplateTags.Text = .TemplateTags

            If txtTemplateTags.Mode = EControlMode.Locked Then
                Dim list As List(Of String) = CUtilities.StringToListStr(.TemplateTags)
                If list.Count > 0 Then txtTemplateTags.Text = String.Concat("[", CUtilities.ListToString(list, String.Concat("]<br>[")), "]")

            End If
        End With
    End Sub
    Protected Sub SaveEmailTemplate()
        With Me.EmailTemplate
            If CSession.IsDeveloper Then
                .TemplateName = txtTemplateName.Text
                If CSession.IsDeveloper Then
                    .TemplateHasSubject = chkTemplateHasSubject.Checked
                    .TemplateIsHtml = chkTemplateIsHtml.Checked
                    .TemplateTags = txtTemplateTags.Text
                End If
            End If
            If txtTemplateSubject.Visible Then .TemplateSubject = txtTemplateSubject.Text
            .TemplateBody = txtTemplateBody.Text

            .Save()
        End With
            'CCache.ClearCache()
    End Sub
#End Region


End Class
